package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.github.pagehelper.PageInfo;
import com.zzgele.qmzb.api.core.Constants;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.TicketSwapLog;
import com.zzgele.qmzb.api.model.User;
import com.zzgele.qmzb.api.service.TicketSwapLogService;
import com.zzgele.qmzb.api.service.UserService;
import com.zzgele.qmzb.api.utils.CalculateUtil;
import com.zzgele.qmzb.api.utils.DateUtil;
import com.zzgele.qmzb.api.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
* Created by Martin on 2017/08/04.
*/
@RestController
@RequestMapping("/ticket")
public class TicketController {
    private final Logger logger = LoggerFactory.getLogger(TicketController.class);
    @DubboConsumer
    private TicketSwapLogService ticketSwapLogService;
    @DubboConsumer
    private UserService userService;
    @PostMapping("/swap") //票转换钻石    输入票
    public Result withdrawals(Long login_uid, String diamond) {
        HashMap hashMap = new HashMap();
        int x=0;
        try{
             x=Integer.parseInt(diamond);
            if(x%10!=0){
                return ResultGenerator.genFailResult("非法数字");
            }

        }   catch (Exception e){
            logger.error(e.getStackTrace().toString());
            return ResultGenerator.genFailResult("非法数字");
        }

        BigDecimal percentage = CalculateUtil.getSubtractBalance(new BigDecimal(1),new BigDecimal(Constants.COMMISSION_CHARGE));
        User user = userService.findById(login_uid);
        BigDecimal userTicket = user.getTicket();
        if(userTicket.intValue()< x) {
            return ResultGenerator.genFailResult("票不足", hashMap);
        }
        hashMap.put("ticket", userTicket);
        HashMap map;
        Double ticket2 =  x*percentage.doubleValue();// divide(percentage); //票

        String message = "票转换钻石成功";
        //扣票，加钻石
        map = new HashMap();
        map.put("change",x);
        map.put("uid", login_uid);
        map.put("surplus",ticket2.intValue() ); //钻石和票1:1  diamond2表示扣除手续费后的票
        userService.setTtd(map);
        user = userService.findById(login_uid);
        BigDecimal d_balance = user.getDiamond();
        BigDecimal t_balance = user.getTicket();
        hashMap.put("diamond", d_balance);
        hashMap.put("ticket", t_balance);
        TicketSwapLog withdrawalsLog = new TicketSwapLog();
        withdrawalsLog.setChanges(new BigDecimal(x));
        withdrawalsLog.setSurplus(new BigDecimal(ticket2.intValue()));
        withdrawalsLog.setUid(login_uid);
        withdrawalsLog.setBalance(t_balance);
        withdrawalsLog.setRemark("");
        withdrawalsLog.setCreateTime(DateUtil.getCurUnixTimeStampInt());
        ticketSwapLogService.save(withdrawalsLog);
        return ResultGenerator.genSuccessResult(message, hashMap);
    }

    @PostMapping("/swap/log")
    public Result log_list(Long login_uid, Integer page, Integer page_size) {
        PageUtil.startPage(page, page_size);
//        Map<String, Object> map = new HashMap();
//        map.put("uid", login_uid);
        List<HashMap> diamondRechargeLogList = ticketSwapLogService.list(login_uid);
        HashMap diamondRechargeLogListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(diamondRechargeLogList));

        return ResultGenerator.genSuccessResult(diamondRechargeLogListHashMap);
    }

}
